package com.demo.connectdbtest.module.gitlab.utils;
import org.gitlab4j.api.models.TreeItem;

import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.RepositoryFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class GitLabApiUtil {
//    GitlabAPI connect = GitlabAPI.connect("http://192.168.159.129:80", "glpat-TGZUk6NqYK2P53fR1G7x");

    private static GitLabApi gitLabApi = new GitLabApi("http://192.168.159.129:80", "glpat-TGZUk6NqYK2P53fR1G7x", null,null);

    public static InputStream getRawFileStreamByProjId(Integer gitProjId, String branch, String filePath) throws GitLabApiException {
        return   gitLabApi.getRepositoryFileApi().getRawFile(gitProjId,branch,filePath);
    }
    public static InputStream getRawFileStreamByGitPath(String gitPath,String branch,String filePath) throws GitLabApiException {
        return   gitLabApi.getRepositoryFileApi().getRawFile(gitPath,branch,filePath);
    }

    public static RepositoryFile getFile(Integer projectId, String filepath, String branch) throws GitLabApiException {
        return gitLabApi.getRepositoryFileApi().getFile(projectId, filepath, branch);
    }

    public static Optional<RepositoryFile> getOptionalFile(Integer projectId, String filepath, String branch) throws GitLabApiException {
        return gitLabApi.getRepositoryFileApi().getOptionalFile(projectId, filepath, branch);
    }

    public static List<Project> getProjects() throws GitLabApiException {
        return gitLabApi.getProjectApi().getProjects();
    }
    //查询文件树结构
    public static List<TreeItem> getFileTree(Integer gitProjId, String branch, String filePath) throws GitLabApiException {
        return gitLabApi.getRepositoryApi().getTree(gitProjId,filePath,branch,true);//默认递归查询
    }
    //获取blob文件列表
    public static List<TreeItem> getFileList(Integer gitProjId, String branch, String filePath) throws GitLabApiException {
        List<TreeItem> tree = gitLabApi.getRepositoryApi().getTree(gitProjId, filePath, branch, true);//默认递归查询  筛选出文件后返回list
        ArrayList<TreeItem> treeItems = new ArrayList<>();
        for (TreeItem treeItem : tree) {
            if ("blob".equals(treeItem.getType().toString()) ) {
                treeItems.add(treeItem);
            }
        }
        return  treeItems;
    }
    public static List<TreeItem> getFileList(String projectPath, String branch, String filePath) throws GitLabApiException {
        return gitLabApi.getRepositoryApi().getTree(projectPath,filePath,branch,true);//默认递归查询  筛选出文件后返回list
    }

    public static synchronized RepositoryFile createFile(Integer gitProjId , RepositoryFile file, String branch, String commit) throws GitLabApiException {
        return  gitLabApi.getRepositoryFileApi().createFile(gitProjId,file,branch,commit);
    }

    public static synchronized RepositoryFile updateFile(Integer gitProjId , RepositoryFile file, String branch, String commit) throws GitLabApiException {
        return  gitLabApi.getRepositoryFileApi().updateFile(gitProjId,file,branch,commit);
    }

    public static RepositoryFile getFileByProjId(Integer gitProjId,String branch,String filePath) throws GitLabApiException {
        return   gitLabApi.getRepositoryFileApi().getFile(gitProjId,filePath,branch);
    }

}
